import cgi
import wsgiref.handlers
import os
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from cgi import parse_qs, escape

####################################
#FUNCTION Name:  getJSONOfString
#Description:Represents any string in javascript format.
#            it can be used for json conversion
#
#
#
#
#Auther:Markandey Singh
####################################
def getJSONOfString(mystr):
    obj=str(mystr)
    obj = obj.replace('\\', r'\\')
    
    #include only if / is to be escaped
    #obj = obj.replace('/', r'\/')
    
    obj = obj.replace('"', r'\"')
    obj = obj.replace('\b', r'\b')
    obj = obj.replace('\f', r'\f')
    obj = obj.replace('\n', r'\n')
    obj = obj.replace('\r', r'\r')
    obj = obj.replace('\t', r'\t')
    obj='"'+obj
    obj=obj+'"'
    return obj
#########################
#       LifeBitModel    #
#########################
class LifeBit(db.Model):
  Date =db.DateTimeProperty(auto_now_add=True,required=True)
  Location = db.StringProperty()
  Label=db.StringProperty()
  Application=db.StringProperty()
  Content = db.StringProperty(multiline=True)
  User = db.UserProperty(required=True)
  URL =db.StringProperty()
  INT=db.IntegerProperty()
  Access=db.IntegerProperty(default=0)
  ####################################
  #FUNCTION Name:  getjson
  #Description:returns the string in json format represening the self object.
  #
  #
  #
  #
  #Auther:Markandey Singh
  ####################################  
  def getjson(self):
        jn="{\n"
        jn=jn+'"Date": '+getJSONOfString(str(self.Date))+'\n,';
        jn=jn+'"Location": '+getJSONOfString(self.Location)+'\n,';
        jn=jn+'"Label": '+getJSONOfString(self.Label)+'\n,';
        jn=jn+'"Application": '+getJSONOfString(self.Application)+'\n,';
        jn=jn+'"Content": '+getJSONOfString(self.Content)+'\n,';
        jn=jn+'"User": '+getJSONOfString(str(self.User))+'\n,';
        jn=jn+'"URL": '+getJSONOfString(self.URL)+'\n,';
        if (self.INT):
          jn=jn+'"INT": '+str(self.INT)+'\n,';
        else:
          jn=jn+'"INT": '+'null'+'\n,';
        jn=jn+'"Access": '+str(self.Access)+'\n}';
        return jn
####################################
#FUNCTION Name:  getjson
#Description:returns the string in json format represening the self object.
#
#
#
#
#Auther:Markandey Singh
####################################    
class GetDataJSON(webapp.RequestHandler):
  def get(self):
    parameters = parse_qs(os.environ.get('QUERY_STRING', ''))
    mydata=None
    if 'q' in parameters:
        QueryString = escape(parameters['q'][0])
        mydata=db.GqlQuery(QueryString);
    else:
        mydata=LifeBit.all();
        
    if users.get_current_user():
      jsontring='pgDraw({\nDATA:{\nITEM:[\n'#START

      for datum in mydata:
        if(datum.User==users.get_current_user()):
            jsontring=jsontring+datum.getjson()+',\n'
      jsontring=jsontring+'{"END":null}\n]\n}\n})'#END
      self.response.out.write(jsontring)
    else:
      print '<b>Anonymous query is not supported..Please login</b>'
